//
//  STTableViewController.swift
//  iPhone
//
//  Created by Toj on 7/11/23.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STTableViewController: UIViewController {
    
    var topInset: CGFloat = .nav_barFullHeight
    
    private var colors = kColors
    
    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.backgroundColor = .white

        view.rowHeight = 44
        view.dataSource = self
        view.delegate = self
        view.register(cellClass: STTableViewCell.self)
        
        return view
    }()
    
    private lazy var clearButton: UIButton = {
       let view = UIButton()
        view.backgroundColor = .random
        view.setTitle("清空", for: .normal)
        view.addTarget(self, action: #selector(didTapClear), for: .touchUpInside)
        return view
    }()
}

// MARK: - 生命周期
extension STTableViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 数据请求
extension STTableViewController {
    
}

// MARK: - 点击事件
@objc private extension STTableViewController {
    
    func didTapClear() {
        colors = []
        tableView.reloadData()
    }
}

// MARK: - Public
extension STTableViewController {
    
}

// MARK: - Private
private extension STTableViewController {
    
}

// MARK: - UITableViewDataSource
extension STTableViewController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return colors.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withReuseIdentifierClass: STTableViewCell.self)
        let pointer = Unmanaged<AnyObject>.passUnretained(cell as AnyObject).toOpaque()
        cell.textLabel?.text = "tableView: address: \(pointer), num:" + String(indexPath.row)

        if indexPath.row < colors.count {
            let colorHex = colors[indexPath.row]
            cell.textLabel?.textColor = .random
            cell.contentView.backgroundColor = colorHex.to_color
        }
        return cell
    }
}

// MARK: - UITableViewDelegate
extension STTableViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath) as? STTableViewCell
        let indexText = cell?.textLabel?.text ?? ""
        print("-->toj: didTap index : \(indexText)")
        
        guard let button = cell?.button else { return }
        var point = view.convert(button.frame, from: button.superview)
        print("-->toj: didTap index : \(point)")
        
        
        point = button.superview?.convert(button.frame, to: nil) ?? .zero
        print("-->toj: didTap index : \(point)")
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return .cg_cipher
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
       return nil
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        
        return .cg_cipher
    }
}

// MARK: - 初始化设置
private extension STTableViewController {
    
    func setupMakeAddSubviews() {
        view.addSubview(tableView)
        view.addSubview(clearButton)
    }
    
    func setupMakeLayoutSubviews() {
        
        tableView.snp.makeConstraints { make in
            make.edges.equalTo(view)
        }
        
        clearButton.snp.makeConstraints { make in
            make.top.equalTo(topInset)
            make.height.equalTo(44)
            make.width.equalTo(100)
            make.centerX.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        
        tableView.reloadData()
        
            tableView.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
        tableView.contentOffset = CGPoint(x: 0, y: -topInset)
    }
}
